package gui;

import gui.houseeditor.HouseModel;
import gui.houseeditor.Stanza;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import attori.sensori.SensoreTemperatura;
import attori.sensori.SensoreVolume;

@SuppressWarnings("serial")
public class DialogSensori extends JDialog {

	JPanel p = new JPanel();
	JPanel p1 = new JPanel();
	HouseModel model;
	Map<Stanza,ButtonGroup> mappa;
	
	public static final int TEMPERATURA=0,VOLUME=2;
	private int dialogType;
	
	public DialogSensori(Frame f, HouseModel model, int dialogType) {
		super(f);
		this.model = model;
		this.dialogType=dialogType;
		mappa=new HashMap<Stanza, ButtonGroup>();
		
		switch (dialogType) {

		case VOLUME:
			setTitle("Sensori Volume");
			this.setIconImage(Toolkit.getDefaultToolkit().getImage("img/lampadinacolore.png"));
			break;
		case TEMPERATURA:
			setTitle("Sensori Temperatura");
			this.setIconImage(Toolkit.getDefaultToolkit().getImage("img/termometro.png"));
			break;
		}
		
		setResizable(false); 
		setModal(true);
		this.setLayout(new BorderLayout());
		p.setLayout(new GridBagLayout());
		init();
		initButtons();
		add(p, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);
		p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
		pack();
		this.setLocationRelativeTo(f);
		setVisible(true);
	}

	private void initButtons() {
		JButton ok = new JButton("OK");
		ok.setAction(new OKAction());
		JButton annulla = new JButton("Annulla");
		annulla.setAction(new AnnullaAction());
		p1.add(ok);
		this.getRootPane().setDefaultButton(ok);
		p1.add(annulla);
	}

	private class AnnullaAction extends AbstractAction {

		public AnnullaAction() {
			super("Annulla");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			DialogSensori.this.dispose();
		}

	}

	private class OKAction extends AbstractAction {

		public OKAction() {
			super("OK");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {

				switch (dialogType) {
				
				case VOLUME:
					for (ButtonGroup button : mappa.values()) {
						AbstractButton ON = button.getElements().nextElement();
						Stanza stanza = getKey(button); 
						if (ON.isSelected()) {
							stanza.getSensoreVolume().send(new SensoreVolume.Accendi());
						}else{
							stanza.getSensoreVolume().send(new SensoreVolume.Spegni());
						}
					}
					break;
				case TEMPERATURA:
					for (ButtonGroup button : mappa.values()) {
						AbstractButton ON = button.getElements().nextElement();
						Stanza stanza = getKey(button); 
						if (ON.isSelected()) {
							stanza.getSensoreTemperatura().send(new SensoreTemperatura.Accendi());
						}else{
							stanza.getSensoreTemperatura().send(new SensoreTemperatura.Spegni());
						}
					}
					break;
				}
				
				dispose();
		}

		private Stanza getKey(ButtonGroup button) {
			for (Stanza st: mappa.keySet()) {
				if(mappa.get(st).equals(button)){
					return st;
				}
			}
			return null;
		}

	}

	private void init() {
		List<Stanza> stanze = model.getStanze();
		int riga = 0;
		for (Stanza s : stanze) {
			JLabel name = new JLabel(s.getNome(), SwingConstants.CENTER);
			p.add(name, new GridBagConstraints(0, riga, 1, 1, 1.0, 1.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(10, 10, 5, 2), 0, 0));
			ButtonGroup group = new ButtonGroup();
			JRadioButton ON = new JRadioButton("ON");
			JRadioButton OFF = new JRadioButton("OFF");
			group.add(ON);
			group.add(OFF);
			
			mappa.put(s,group);
			
			switch (dialogType) {
			case VOLUME:
				if(s.getSensoreVolume().currentStatus()== SensoreVolume.OPERATIVE ){
					group.setSelected(ON.getModel(),true);
				}else{
					group.setSelected(OFF.getModel(),true);
				}
				break;
			case TEMPERATURA:
				if(s.getSensoreTemperatura().currentStatus()== SensoreTemperatura.OPERATIVE ){
					group.setSelected(ON.getModel(),true);
				}else{
					group.setSelected(OFF.getModel(),true);
				}
				break;
			}
			p.add(ON, new GridBagConstraints(1, riga, 1, 1, 1.0, 1.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(10, 0, 5, 10), 0, 0));
			p.add(OFF, new GridBagConstraints(2, riga, 1, 1, 1.0, 1.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(10, 0, 5, 10), 0, 0));
			riga++;

		}
	}

//	public static void main(String[] args) {
//		JFrame f = new JFrame();
//		f.setVisible(true);
//		Stanza s1 = new Stanza("carlo", 23, null, 0, 0, 0, 0);
//		Stanza s2 = new Stanza("peppe", 23, null, 0, 0, 0, 0);
//		List<Stanza> s = new LinkedList<Stanza>();
//		s.add(s1);
//		s.add(s2);
//		HouseModel = new HouseModel(s);
//		new DialogSensori(f, model, VOLUME);
//		
//	}
}